<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
if(!check_admin("commission_technique,commission_arb_marq")) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = "email_admin";
	if(check_admin("commission_technique")){
		$equipes = liste('equipes',"id_vbcll","num");
		$equipes = array_combine(array_values($equipes),array_values($equipes));
		$others = array("arbitres" => "arbitres", "marqueurs" => "tous les marqueurs", "marqueurs_sans" => 
		"marqueurs non inscrits","tous" =>"<span class = 'bold'>TOUS</span>");
		$categories = array_merge($equipes,$others);		
	}
	elseif(check_admin("commission_arb_marq")){
		$categories = array("arbitres" => "arbitres", "marqueurs" => "tous les marqueurs", "marqueurs_sans" => 
		"marqueurs non inscrits");
		
	}
	else $categories = array();
	if(!empty($_REQUEST['submit'])){
		require_once("include/htmlMimeMail.php");
		$selected = array();
		$TOs = array();
		$missings = array();
		foreach($categories as $id => $print){
			if(!empty($_REQUEST[$id])) $selected[$id] = $print;
		}
		if(!empty($selected['tous'])){
			$q = "select id, prenom, nom , email from membres where valide = 1 and fictif = 0";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if(strpos($s->email,"@") === FALSE) $missings[$s->id] = $s->prenom." ".$s->nom;
				else $TOs[$s->id] = $s->email;
			}		
		}
		else{
			foreach($equipes as $id => $id_vbcll){
				if(!empty($selected[$id_vbcll])){
					$equipe = new equipe($id,"id_vbcll");
					$membres = $equipe->list_membres();
					foreach($membres as $membre_id => $membre){
						if(strpos($membre->email,"@") === FALSE) $missings[$membre_id] = $membre->prenom." ".$membre->nom;
						else $TOs[$membre_id] = $membre->email;
					}
				}
			}
			if(!empty($selected['arbitres'])){
				$q = "select id, prenom, nom , email from membres where (arbitre = 1 or arbitre_junior = 1) and valide = 1 and fictif = 0";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					if(strpos($s->email,"@") === FALSE) $missings[$s->id] = $s->prenom." ".$s->nom;
					else $TOs[$s->id] = $s->email;
				}
			}
			if(!empty($selected['marqueurs'])){
				$q = "select id, prenom, nom , email from membres where marqueur = 1 and valide = 1 and fictif = 0";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					if(strpos($s->email,"@") === FALSE) $missings[$s->id] = $s->prenom." ".$s->nom;
					else $TOs[$s->id] = $s->email;
				}		
			}
			if(!empty($selected['marqueurs_sans'])){
				$q = "select id, prenom, nom , email from membres where marqueur = 1 and arbitre = 0 and arbitre_junior = 0 and valide = 1  and fictif = 0";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					$q1 = "select id from membre_equipes where membre_id = $s->id";
					$r1 = ExecRequete($q1);
					if(mysql_num_rows($r1)){
						$q1 = "select id from membre_commissions where membre_id = $s->id";
						$r1 = ExecRequete($q1);
						if(!mysql_num_rows($r1)){
							$q1 = "select count(*) as nb from matchs where marqueur1 = $s->id or marqueur2 = $s->id";
							$r1 = ExecRequete($q1);
							$s1 = LigneSuivante($r1);
							if($s1->nb < 2){
								if(strpos($s->email,"@") === FALSE) $missings[$s->id] = $s->prenom." ".$s->nom;
								else $TOs[$s->id] = $s->email;				
							}					
						}					
					}
				}		
			}		
		}

		entete('Email');
		echo "<h1>R&eacute;sultat de l'envoi</h1>\n";
		if(count($TOs)){
			$mail = new htmlMimeMail();
			$de = $_REQUEST['de'];
			$titre = $_REQUEST['titre'];
			$corps = $_REQUEST['corps']."\r\n";
			if(get_magic_quotes_gpc()) $corps = stripslashes($corps);
			if(!empty($_FILES['fichier'])){
				if(file_exists($_FILES['fichier']['tmp_name'])){
					$attachment = $mail->getFile($_FILES['fichier']['tmp_name']);	
					$mail->addAttachment($attachment, $_FILES['fichier']['name'], $_FILES['fichier']['type']);					
				}
			}
			$mail->setText(wordwrap($corps, 70)); 
			$mail->setFrom($de);
			$mail->setSubject($titre);             
			$mail->setBcc(implode(",",$TOs));
			$result = $mail->send(array("info@vbcll.ch"));
			echo "<h2>Envois r&eacute;ussis</h2>\n";
			echo "<p class = 'center bold'>".(count($TOs))." emails ont &eacute;t&eacute; envoy&eacute;s</p>\n";
			echo "<h2>Envois &eacute;chou&eacute;s</h2>\n";
			echo "<p class = 'center bold'>Les personnes suivantes n'ont pas d'adresse email : </p>\n";
			echo "<ul class = 'float'>\n";
			foreach($missings as $id => $nom) echo "<li class = 'float'>$nom</li>\n";
			echo "</ul>\n";
			echo "<br class = 'clear' />\n";
		}
		else echo "<p class = 'error'>Aucun email n'a &eacute;t&eacute; envoy&eacute; (aucun membre s&eacute;l&eacute;ctionn&eacute;)</p>";
		echo "<p class = 'center'><button type = 'button' onclick = 'self.location.href=\"email_admin.php\"'>retour</button></p>\n";
		pied();
	}
	else{
		$script = "function check(myform){\n";
		$script .= "	if(document.myform.de.value.indexOf('@') <= 0){\n";
		$script .= "		alert('Vous n\'avez pas fourni d\'email valide !!');\n";
		$script .= "		document.myform.de.focus();\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	else if(document.myform.titre.value == ''){\n";
		$script .= "		alert('Vous n\'avez pas fourni de titre !!');\n";
		$script .= "		document.myform.titre.focus();\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	else if(document.myform.corps.value == ''){\n";
		$script .= "		alert('Vous n\'avez pas fourni de texte !!');\n";
		$script .= "		document.myform.corps.focus();\n";
		$script .= "		return false;\n";
		$script .= "	}\n";
		$script .= "	return true;\n";
		$script .= "}\n";


		entete("Email", $script,"",1);
		echo "<h1>Envoi d'email</h1>\n";
		echo "<form action = 'email_admin.php' method = 'post' onsubmit = 'return check(this)' name = 'myform' id = 'myform' enctype = 'multipart/form-data'>\n";
		echo "<h2>Choisir une cat&eacute;gorie de personnes : </h2>\n";
		echo "<p class = 'center'><ul class = 'float'>\n";
		foreach($categories as $id => $print){
			echo "<li class = 'float'><input type = 'checkbox' name = '$id' value = '1' />$print</li>\n";
		}
		echo "</ul></p>\n";
		echo "<br class = 'clear' />\n";
		echo "<h2>Ecrire le message</h2>\n";
		$user_id = check_cookie();
		$membre = new membre($user_id);
		$de = "<input type = 'text' size = '45' name = 'de' value = '$membre->email' />";
		$titre = "<input type = 'text' size = '45' name = 'titre' value = '' />";
		$corps = "<textarea name = 'corps' cols = '45' rows = '20'></textarea>\n";
		$titre_box = "Email";
		$fichier = "<input type = 'file' name = 'fichier' />\n";
		$content = leftCol('De',25);
		$content .= rightCol($de,73);
		$content .= leftCol("Titre",25);
		$content .= rightCol($titre,73);	
		$content .= leftCol("Corps",25);
		$content .= rightCol($corps,73);
		$content .= leftCol("Fichier attach&eacute;",25);
		$content .= rightCol($fichier,73);
		$content .= "<br class = 'clear' />\n";
		echo box($titre_box,$content,600,1);
		echo "<input type = 'hidden' name = 'MAX_FILE_SIZE' value = '1000000' />\n";
		echo "<p class = 'center'><button type = 'submit' name = 'submit' value = '1'>Envoyer</button></p>\n";
		echo "</form>\n";
		pied();	
	}	
}
?>
